import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'
// import { Observable } from 'rxjs'
import { PrismaService } from 'src/prisma/prisma.service'
@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private prisma: PrismaService) {}
  async canActivate(context: ExecutionContext) {
    const request = context.switchToHttp().getRequest()
    let role
    if (request.user) {
      const user = await this.prisma.user.findUnique({
        where: {
          id: request.user.userId,
        },
      })
      console.log(user)
      role = (user as any).role ?? 1
    }
    return role === 1
  }
}
